package com.tz.cartelo.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.tz.cartelo.BaseApplication;
import com.tz.cartelo.R;
import com.tz.cartelo.adapter.GoodsClassifyAdapter;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.model.GoodsType;
import com.tz.cartelo.result.ResultGoodsClassify;
import com.tz.cartelo.util.CommonUtils;
import com.tz.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * Created by Administrator on 2017/4/16.
 */

public class GoodsClassifyActivity extends DataLoadActivity implements View.OnClickListener, BGARefreshLayout.BGARefreshLayoutDelegate {
    private static final String TAG = "GoodsClassifyActivity";
    private BGARefreshLayout mRefreshLayout;
    private TextView mGoodsClassifySearchText;
    private Api mApi;
    private GoodsClassifyAdapter mGoodsClassifyAdapter;
    private List<GoodsType> mGoodsTypeList;
    private int mPageNo;
    private boolean mCanLoadMore = true;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_goods_classify;
    }

    @Override
    protected void init() {
        BaseApplication.addActivity(this);
        mRefreshLayout = (BGARefreshLayout) findViewById(R.id.fresh_layout);
        mRefreshLayout.setDelegate(this);
        CommonUtils.setBGARefreshLayout(mRefreshLayout, this);

        mGoodsClassifySearchText = (TextView) findViewById(R.id.goods_classify_search_text);
        mGoodsClassifySearchText.setOnClickListener(this);
        ListView goodsClassifyList = (ListView) findViewById(R.id.goods_classify_list);
        mGoodsTypeList = new ArrayList<>();
        mGoodsClassifyAdapter = new GoodsClassifyAdapter(this, mGoodsTypeList);
        goodsClassifyList.setAdapter(mGoodsClassifyAdapter);

        mGoodsClassifyAdapter.setOnClassifyDetailsClickListener(new GoodsClassifyAdapter.OnClassifyDetailsClickListener() {
            @Override
            public void onUserClick(int position, int positionDetails) {
                Bundle bundle = new Bundle();
                bundle.putString(Key.GOODS_CLASSIFY_ID, mGoodsTypeList.get(position).getGoodsCategoryList().get(positionDetails).getGoodsCategoryId());
                bundle.putString(Key.GOODS_CLASSIFY_TITLE, mGoodsTypeList.get(position).getGoodsCategoryList().get(positionDetails).getTitle());
                if (getIntent().getFlags() == Key.CLASSIFY_GOODS)
                    switchActivity(GoodsClassifyDetailsActivity.class, bundle, Key.SEARCH_GOODS_STYLE);
                else
                    switchActivity(ManagerStockActivity.class, bundle, Key.CLASSIFY_STOCK);
            }
        });

        mPageNo = 1;
        mGoodsTypeList.clear();
        mApi = Api.GOODS_CLASSIFY;
        loadData(mApi, true);
    }

    @Override
    protected void initTitle() {
        View stock_classify_title = findViewById(R.id.stock_classify_title);
//        findViewById(R.id.goods_classify_title_back).setOnClickListener(this);
        findViewById(R.id.goods_classify_title_back_layout).setOnClickListener(this);
        LinearLayout goods_classify_title = (LinearLayout) findViewById(R.id.goods_classify_title);
        if (getIntent().getFlags() == Key.CLASSIFY_GOODS) {
            stock_classify_title.setVisibility(View.GONE);
            goods_classify_title.setVisibility(View.VISIBLE);
        } else {
            stock_classify_title.setVisibility(View.VISIBLE);
            goods_classify_title.setVisibility(View.GONE);
            TextView titleText = (TextView) findViewById(R.id.title_text);
            titleText.setText(R.string.manager_stock_classify_title);

            ImageView titleBack = (ImageView) findViewById(R.id.title_back);
            titleBack.setVisibility(View.VISIBLE);
            titleBack.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
            case R.id.goods_classify_title_back_layout:
                finish();
                break;
            case R.id.goods_classify_search_text:
                switchActivity(GoodsClassifySearchActivity.class, null);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case GOODS_CLASSIFY:
                params.addParam("pageNo", mPageNo + "");
                break;
            default:
                break;
        }

    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case GOODS_CLASSIFY:
                //结束刷新和加载
                mRefreshLayout.endRefreshing();
                mRefreshLayout.endLoadingMore();

                ResultGoodsClassify resultGoodsClassify = fromJson(response, ResultGoodsClassify.class);
                if (!resultGoodsClassify.isSuccess()) {
                    showToast(resultGoodsClassify.getMessage());
                    return;
                }
                if (null != resultGoodsClassify.getGoodsTypeResult().getGoodsTypeList())
                    mGoodsTypeList.addAll(resultGoodsClassify.getGoodsTypeResult().getGoodsTypeList());
                mGoodsClassifyAdapter.notifyDataSetChanged();
                if (mPageNo > 1) {
                    if (null == resultGoodsClassify.getGoodsTypeResult() || null == resultGoodsClassify.getGoodsTypeResult().getGoodsTypeList())
                    {
                        mCanLoadMore = false;
                        mPageNo--;
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        mPageNo = 1;
        mGoodsTypeList.clear();
        mCanLoadMore = true;
        loadData(Api.GOODS_CLASSIFY, true);
        mRefreshLayout.endRefreshing();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (!mCanLoadMore) {
            mRefreshLayout.endLoadingMore();
            return false;
        } else {
            mPageNo++;
            loadData(Api.GOODS_CLASSIFY, true);
            return true;
        }
    }
}
